﻿using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace QFramework.Pacman
{
	public class UIScript : MonoBehaviour
	{

		public int high, score;

		public List<Image> lives = new List<Image>(3);

		Text txt_score, txt_high, txt_level;


		[Inject]
		public ScoreManager ScoreManager { get; set; }

		private void Awake()
		{
			PacmanGame.Container.RegisterInstance(this);
		}

		// Use this for initialization
		void Start()
		{
			PacmanGame.Container.Inject(this);

			txt_score = GetComponentsInChildren<Text>()[1];
			txt_high = GetComponentsInChildren<Text>()[0];
			txt_level = GetComponentsInChildren<Text>()[2];

			for (int i = 0; i < 3 - this.Model().Lives.Value; i++)
			{
				Destroy(lives[lives.Count - 1]);
				lives.RemoveAt(lives.Count - 1);
			}
			
			var model = PacmanGame.Container.Resolve<PacmanModel>();
			model.Score.OnValueChanged += OnScoreChanged;

		}

		void OnScoreChanged(int score)
		{
			this.score = score;
			txt_score.text = "Score\n" + score;
		}

		private void OnDestroy()
		{
			var model = PacmanGame.Container.Resolve<PacmanModel>();
			model.Score.OnValueChanged -= OnScoreChanged;
		}

		// Update is called once per frame
		void Update()
		{
			high = ScoreManager.High();

			// update score text

			txt_high.text = "High Score\n" + high;
			txt_level.text = "Level\n" + (this.Model().Level.Value + 1);
		}
	}
}